package com.ebupt.migu.music.autoTri.Controller;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ebupt.migu.music.autoTri.Entity.SingerBlackListBean;
import com.ebupt.migu.music.autoTri.Service.SingerBlackListService;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MiguRuntimeException;
import com.ebupt.migu.music.common.util.ReqestParamUtil;

/**  歌手黑名单
 * @author xuhongsong
 * @version 1.0
 * @date 2020/10/10 14:50
 */
@RestController
@RequestMapping("/singerBlackList")
public class SingerBlackListController {


    @Resource
    private SingerBlackListService singerBlackListService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IDAuthUtils idAuthUtils;

    @PostMapping("/add")
    public ResultObject add() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        ResultObject restResponse = new ResultObject();
        Map<String, Object> maps = ReqestParamUtil.getParamsWithUserInfo(request);
        String singerName = null;
        String remark = null;
        Long singerId = null;
        String csvUrl = null;
        int type=Integer.parseInt(maps.get("type").toString());
        switch (type){
            case 1:
                singerName = maps.get("singerName").toString();
                if (maps.get("remark") != null && maps.get("remark") != "") {
                    remark = maps.get("remark").toString();
                }
                if (maps.get("singerId")!=null && maps.get("singerId")!="") {
                    if(!isNumeric(maps.get("singerId").toString())){
                     restResponse.setCode("6666");
                     restResponse.setMsg("歌手id格式不正确！");
                     return restResponse;
                    }
                    singerId = Long.valueOf(maps.get("singerId").toString());
                    break;
                }
            case 2:
                csvUrl = maps.get("csvUrl").toString();
                break;
        }
        try {
            SingerBlackListBean singerBlackListBean = new SingerBlackListBean(singerId, singerName,
                    remark, type, csvUrl, maps.get("creatorName").toString(),
                    maps.get("creatorName").toString(),maps.get("creatorId").toString(),maps.get("creatorDepartmentId").toString());
            singerBlackListService.add(singerBlackListBean);
            restResponse.setCode("0000");
            restResponse.setMsg("创建歌手黑名单成功！");
        } catch (MiguRuntimeException e) {
            switch (type) {
                case 1:
                    restResponse.setCode("9999");
                    restResponse.setMsg("歌手黑名单id已存在！");
                    break;
                case 2:
                    restResponse.setCode("8888");
                    restResponse.setMsg("模板不正确！");
                    break;
            }
        } catch (NumberFormatException e) {
            restResponse.setCode("7777");
            restResponse.setMsg("数据不正确！");
        }
        return restResponse;
    }


    /**
     * 删除黑名单
     *
     * @param dataIds
     * @return
     */
    @PostMapping("/delete")
    public ResultObject delete(@RequestParam("dataIds") String dataIds) {
        ResultObject restResponse = new ResultObject();
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        int code = singerBlackListService.delete(dataIds.split(","), params.get("creatorName").toString());
        if (code >= 1) {
            restResponse.setCode("0000");
            restResponse.setMsg("删除歌手黑名单成功！");
            restResponse.setData(code);
        } else {
            restResponse.setCode("1110");
            restResponse.setMsg("服务器错误！");
            restResponse.setData(-1);
        }

        return restResponse;
    }

    /**
     * 查询歌手黑名单列表
     *
     * @return
     */
    @GetMapping("/query")
    public ResultObject query() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        return singerBlackListService.queryMap(params);
    }


    /**
     * 判断是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        //Pattern pattern = Pattern.compile("^-?[0-9]+"); //这个也行
            Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");//这个也行
            Matcher isNum = pattern.matcher(str);
            if (!isNum.matches()) {
                return false;
            }
            return true;
    }



}
